﻿Imports System.Data
Imports PadovadoAPI

Partial Class WebUserControls_CntlNews
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CaricaNews()
            AggiornaNEventi()
        End If
    End Sub

    Public Sub CaricaNews()
        Dim CurrentDate As Date = Date.Now
        Dim sql As String = "SELECT * " & _
                         " FROM News WHERE Deleted = 0 AND (DataInizioValidita = CAST('" & CurrentDate.ToString("yyyyMMdd") & "' AS DATE)" & _
                    " OR (DataInizioValidita < CAST('" & CurrentDate.ToString("yyyyMMdd") & "' as date) " & _
                    " AND DataFineValidita >= CAST('" & CurrentDate.ToString("yyyyMMdd") & "' as date))) " & _
                    " ORDER BY Priorita ASC, DataInizioValidita ASC, DataInserimento ASC "
        Dim ds As New DataSet
        Dim dbO As New DBObject
        'Throw New Exception(sql)
        ds = dbO.FillDS(sql, Nothing, 1)
        If IsNothing(ds) OrElse IsNothing(ds.Tables(0)) OrElse ds.Tables(0).Rows.Count <= 0 Then
            LitNews.Text = "<p>Non ci sono altre news. Goditi tutti gli eventi segnalati</p>"
        Else
            For Each r As DataRow In ds.Tables(0).Rows
                '   Throw New Exception(r.Item("DataInserimento"))
                LitNews.Text &= "<p> - " & r.Item("Text") & " - news del " & r.Item("DataInserimento") & "</p>"
            Next
        End If
    End Sub

    Public Sub AggiornaNEventi()

        Dim dbConnector As New DBObject
        Dim CurrentDate As DateTime = DateTime.Now.Date()
        Dim sql As String = ""

        sql = "SELECT COUNT (*) FROM Evento " & _
               " WHERE Deleted= 0 AND DataFine >= CAST('" & CurrentDate.ToString("yyyyMMdd") & "' AS DATE)"
        'Throw New Exception(sql)
        Dim count As Integer

        count = dbConnector.ExecuteScalar(sql, Nothing, 1)

        LitNews.Text &= "<p> - anche oggi on line " & count & " eventi...</p>"

    End Sub
End Class
